package com.darrenchan.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2019-12-10 15:20
 */
public class Q39 {
    public List<List<Integer>> combinationSum(int[] nums, int target) {
        List<List<Integer>> results = new ArrayList<>();
        if (nums == null) {
            return results;
        }
        if (nums.length == 0) {
            results.add(new ArrayList<>());
            return results;
        }

        Arrays.sort(nums);
        dfs(nums, 0, new ArrayList<>(), results, target);

        return results;
    }

    //当然也可以使用sum(nums)来判断和是否等于target，但是传参target更好
    private void dfs(int[] nums, int index, List<Integer> subsets, List<List<Integer>> results, int target) {
        if(target == 0){
            results.add(new ArrayList<>(subsets));
        }

        for (int i = index; i < nums.length; i++) {
            if(target < nums[i]){
                break;
            }
            if(i > index && nums[i] == nums[i - 1]) continue;
            subsets.add(nums[i]);
            dfs(nums, i, subsets, results, target - nums[i]);
            subsets.remove(subsets.size() - 1);
        }
    }
}
